package com.itany.corejava.day16;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServerC {

    public static void main(String[] args) {
        System.out.println("服务器");


        try(ServerSocket ss=new ServerSocket(8888)){

            while(true){
                Socket s=null;
                try{

                    s=ss.accept();
                    new SocketThread(s).start();


                }catch (Exception e){
                    e.printStackTrace();
                }
            }


        }catch (Exception e){
            e.printStackTrace();
        }


    }


    private static class SocketThread extends Thread{

        private Socket s;

        public SocketThread(Socket s) {
            this.s = s;
        }

        @Override
        public void run() {

            try(BufferedReader br=new BufferedReader(
                    new InputStreamReader(
                            s.getInputStream()
                    )
                );
                PrintWriter pw=new PrintWriter(
                        s.getOutputStream()
                )){

                String name=br.readLine();

                if("abc".equals(name)){
                    Thread.sleep(999999);
                }


                pw.println("Hello "+name+"!");

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(s!=null){
                    try {
                        s.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }




        }
    }















}
